package com.winit.openapi.wh.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.bpartner.vo.Bpartner;
import com.winit.oms.spi.inbound.InboundOnTimeRateService;
import com.winit.oms.spi.inbound.command.InboundOnTimeRateQueryCommand;
import com.winit.oms.spi.inbound.vo.InboundOnTimeRateReportVo;
import com.winit.oms.spi.inbound.vo.InboundOnTimeRateVo;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

/**
 * 海外仓入库及时率信息
 * 
 * @version <pre>
 * Author   Version     Date        Changes
 * youming.fan    1.0         2018-3-6   Created
 * 
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/wh/inboundOnTimeRate")
public class InboundOnTimeRateController extends BaseController {

    @Resource
    private InboundOnTimeRateService inboundOnTimeRateService;
    
    @RequestMapping(value = "/queryInboundOnTimeRate")
    public String queryInboundOnTimeRate(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        InboundOnTimeRateQueryCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = new InboundOnTimeRateQueryCommand();
            InboundOnTimeRateVo vo = new InboundOnTimeRateVo();
            vo.setTransportType(json.getString("transportType"));
            vo.setWarehouseEntryType(json.getString("warehouseEntryType"));
            vo.setDestinationWhCode(json.getString("destinationWhCode"));
            Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
            vo.setCustomerCode(bpartner.getValue());
            command.setVo(vo);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        command.setCtx(CommandContext.getContext());
        List<InboundOnTimeRateReportVo> result = inboundOnTimeRateService.queryInboundOnTimeRateList(command);
        if (!CollectionUtils.isEmpty(result)) {
            List<Map<String, Object>> datalist = buildOnTimeRateReportList(result);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("inboundOnTimeRateList", datalist);
            responseMsg.setData(data);
        }
        return SUCCESS;
    }
    
    /**
     * 加载数据
     * @param result
     * @return
     */
    private List<Map<String, Object>> buildOnTimeRateReportList(List<InboundOnTimeRateReportVo> result){
        List<Map<String, Object>> rslist = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = null;
        for (InboundOnTimeRateReportVo inboundOnTimeRateReportVo : result) {
            map = new HashMap<String, Object>();
            map.put("orderDate", inboundOnTimeRateReportVo.getOrderDate());
            map.put("qtyOts", inboundOnTimeRateReportVo.getQtyOts() == null ? 0 :inboundOnTimeRateReportVo.getQtyOts());
            map.put("qtyOvts", inboundOnTimeRateReportVo.getQtyOvts() == null ? 0 :inboundOnTimeRateReportVo.getQtyOvts());
            map.put("qtyUsnor", inboundOnTimeRateReportVo.getQtyUsnor() == null ? 0 :inboundOnTimeRateReportVo.getQtyUsnor());
            map.put("qtyUsovt", inboundOnTimeRateReportVo.getQtyUsovt() == null ? 0 :inboundOnTimeRateReportVo.getQtyUsovt());
            rslist.add(map);
        }
        return rslist;
    }
}
